<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.2" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="javascript,string," />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.2" />






<meta name="description" content="chatAt()1str.charAt(n) 返回字符串的第 n 个字符，如果不在 0~str.length-1之间，则返回一个空字符串。 123var str = &quot;javascript&quot;;str.charAt(2); //&apos;v&apos;str.charAt(12); //&apos;&apos;">
<meta name="keywords" content="javascript,string">
<meta property="og:type" content="article">
<meta property="og:title" content="JS字符串的常用方法">
<meta property="og:url" content="http://yoursite.com/2016/12/18/JS字符串常用方法/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="chatAt()1str.charAt(n) 返回字符串的第 n 个字符，如果不在 0~str.length-1之间，则返回一个空字符串。 123var str = &quot;javascript&quot;;str.charAt(2); //&apos;v&apos;str.charAt(12); //&apos;&apos;">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2017-09-09T09:56:33.364Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="JS字符串的常用方法">
<meta name="twitter:description" content="chatAt()1str.charAt(n) 返回字符串的第 n 个字符，如果不在 0~str.length-1之间，则返回一个空字符串。 123var str = &quot;javascript&quot;;str.charAt(2); //&apos;v&apos;str.charAt(12); //&apos;&apos;">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.2',
    sidebar: {"position":"left","display":"post","offset":12,"offset_float":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2016/12/18/JS字符串常用方法/"/>





  <title>JS字符串的常用方法 | Hexo</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Hexo</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2016/12/18/JS字符串常用方法/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="shine-wl">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="http://dynamic-image.yesky.com/300x-/uploadImages/upload/20140912/upload/201409/wylnwjynkxijpg.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Hexo">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">JS字符串的常用方法</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2016-12-18T14:27:19+08:00">
                2016-12-18
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="chatAt"><a href="#chatAt" class="headerlink" title="chatAt()"></a>chatAt()</h1><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">str.charAt(n)</div></pre></td></tr></table></figure>
<p>返回字符串的第 n 个字符，如果不在 0~str.length-1之间，则返回一个空字符串。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> str = <span class="string">"javascript"</span>;</div><div class="line">str.charAt(<span class="number">2</span>); <span class="comment">//'v'</span></div><div class="line">str.charAt(<span class="number">12</span>); <span class="comment">//''</span></div></pre></td></tr></table></figure>
<a id="more"></a>
<h1 id="charCodeAt"><a href="#charCodeAt" class="headerlink" title="charCodeAt()"></a>charCodeAt()</h1><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">str.charCodeAt()</div></pre></td></tr></table></figure>
<p>charCodeAt()返回的是给定位置字符的字符编码。它可以返回指定位置字符的 Unicode 编码，这个返回值的范围是0 - 65535 之间的整数。通过这个方法，我可以知道我的名字在计算机中是用什么数字存储的。它的取值范围跟上面的charAt( )一样。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> code = <span class="string">"future"</span>;</div><div class="line"><span class="built_in">console</span>.log(code.charAt(<span class="number">2</span>)); <span class="comment">//t</span></div><div class="line"><span class="built_in">console</span>.log(code.charCodeAt(<span class="number">2</span>)) <span class="comment">//116</span></div></pre></td></tr></table></figure>
<h1 id="fromCharCode"><a href="#fromCharCode" class="headerlink" title="fromCharCode( )"></a>fromCharCode( )</h1><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">str.fromCharCode( )</div></pre></td></tr></table></figure>
<p>静态方法String.fromCharCode( )。跟上面的正好是相反的操作，知道Unicode编码来得到相应的字符。它可以接收一个或者多个参数。这里列出一些常见字符的编码范围，</p>
<p>0-9对应编码是48-57</p>
<p>A-Z对应编码是65-90</p>
<p>a-z对应编码是97-122</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">String</span>.fromCharCode(<span class="number">49</span>) <span class="comment">// 1</span></div><div class="line"><span class="built_in">String</span>.fromCharCode(<span class="number">65</span>) <span class="comment">// A</span></div><div class="line"><span class="built_in">String</span>.fromCharCode(<span class="number">97</span>) <span class="comment">// a</span></div></pre></td></tr></table></figure>
<h1 id="indexOf"><a href="#indexOf" class="headerlink" title="indexOf()"></a>indexOf()</h1><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">str.indexOf(substr[,start])</div></pre></td></tr></table></figure>
<p>返回 substr 在字符串 str 中首次出现的位置索引,从 start 位置开始查找，如果不存在，则返回 -1。 </p>
<p>start可以是任意整数，默认值为 0。如果 start &lt; 0 则查找整个字符串（如同传进了 0）。如果 start &gt;= str.length，则该方法返回 -1，除非被查找的字符串是一个空字符串，此时返回 str.length.</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> str = <span class="string">"javascript"</span>;</div><div class="line">str.indexOf(<span class="string">'s'</span>); <span class="comment">// 1</span></div><div class="line">str.indexOf(<span class="string">'s'</span>,<span class="number">6</span>); <span class="comment">// -1</span></div><div class="line">str.indexOf(<span class="string">''</span>,<span class="number">11</span>); <span class="comment">// 10</span></div><div class="line">str.indexOf(<span class="string">''</span>,<span class="number">8</span>); <span class="comment">// 8</span></div></pre></td></tr></table></figure>
<h1 id="lastIndexOf"><a href="#lastIndexOf" class="headerlink" title="lastIndexOf()"></a>lastIndexOf()</h1><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">str.lastIndexOf(substr[,start])</div></pre></td></tr></table></figure>
<p>返回 substr 在字符串 str 中最后出现的位置索引,从 start 位置 向前开始查找，如果不存在，则返回 -1</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="string">'lastindex'</span>.lastIndexOf(<span class="string">'a'</span>,<span class="number">5</span>); <span class="comment">// 1</span></div><div class="line"><span class="string">'lastindex'</span>.lastIndexOf(<span class="string">'a'</span>,<span class="number">0</span>); <span class="comment">// -1</span></div></pre></td></tr></table></figure>
<h1 id="substring"><a href="#substring" class="headerlink" title="substring()"></a>substring()</h1><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">str.substring(start[, end])</div></pre></td></tr></table></figure>
<p>返回从 start 到 end（不包括）之间的字符，start、end均为 非负整数。若结束参数(end)省略，则表示从start位置一直截取到最后。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> str = <span class="string">'abcdefg'</span>;</div><div class="line">str.substring(<span class="number">1</span>, <span class="number">4</span>); <span class="comment">//"bcd"</span></div><div class="line">str.substring(<span class="number">1</span>); <span class="comment">// "bcdefg"</span></div><div class="line">str.substring(<span class="number">-1</span>); <span class="comment">//"abcdefg" 传入负值时会视为0</span></div></pre></td></tr></table></figure>
<h1 id="slice"><a href="#slice" class="headerlink" title="slice()"></a>slice()</h1><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">str.slice(start[,end])</div></pre></td></tr></table></figure>
<p>返回从 start 到 end （不包括）之间的字符，可传负值</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> str = <span class="string">'this is awesome'</span>;</div><div class="line">str.slice(<span class="number">4</span>, <span class="number">-1</span>); <span class="comment">//" is awesom"</span></div></pre></td></tr></table></figure>
<h1 id="substr"><a href="#substr" class="headerlink" title="substr()"></a>substr()</h1><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">str.slice(start[,end])</div></pre></td></tr></table></figure>
<p>返回 str 中从指定位置开始到指定长度的子字符串，start可为负值</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> str = <span class="string">"Just give me a reason"</span>;</div><div class="line">str.substr(<span class="number">5</span>, <span class="number">10</span>); <span class="comment">// "give me a "</span></div><div class="line">str.substr(<span class="number">-4</span>, <span class="number">2</span>); <span class="comment">// "as"</span></div></pre></td></tr></table></figure>
<h1 id="substring-slice-substr-三者的区别"><a href="#substring-slice-substr-三者的区别" class="headerlink" title="substring() slice() substr()三者的区别"></a>substring() slice() substr()三者的区别</h1><p>基于子字符串创建新字符串：slice()、substr()和substring().这三个方法都返回操纵字符串的一个子字符串，而且都接收一到两个参数。<br>第一个参数指定字符串的开始位置，第二个参数（可省略）表示子字符串到哪里结束。slice()和substring()的第二个参数指定的是子字符串最后一个字符后面的位置，而substr()的第二个参数指定的是返回的字符个数。如果省略第二个参数，则截取到字符串最后一个位置。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> str = <span class="string">"We are family"</span>; <span class="comment">//length为11</span></div><div class="line"><span class="built_in">console</span>.log(str.slice(<span class="number">2</span>,<span class="number">5</span>));    <span class="comment">// ar(从第2个位置开始，不包含第5个位置)</span></div><div class="line"><span class="built_in">console</span>.log(str.slice(<span class="number">1</span>));      <span class="comment">//e are family（从第1个位置开始，一直到最后）</span></div><div class="line"><span class="built_in">console</span>.log(str.substring(<span class="number">3</span>));  <span class="comment">//are family（从第3个位置开始，一直到最后）</span></div><div class="line"><span class="built_in">console</span>.log(str.substring(<span class="number">3</span>,<span class="number">5</span>));<span class="comment">//ar（从第3个位置开始，不包含第5个位置）</span></div><div class="line"><span class="built_in">console</span>.log(str.substr(<span class="number">2</span>));     <span class="comment">// are family（从第2个位置开始，一直到最后）</span></div><div class="line"><span class="built_in">console</span>.log(str.substr(<span class="number">2</span>,<span class="number">6</span>));   <span class="comment">// are f(从第2个位置开始，截取6个字符)</span></div></pre></td></tr></table></figure>
<p>slice()和substring()方法在正值情况下，实现的功能一致，但是在传递的值为负值时，行为就不相同了。<br>slice()方法会将传入的负值与字符串的长度相加（因此，如果两个参数都会负值，第二个参数必须要大于第一个参数，否则返回的是空字符串）<br>substr()方法会将负的第一个参数加上字符串的长度，将将第二个参数转换为0。（如果第二个参数为负值，返回空的字符串）<br>substring()方法将所有的负值参数都转换为0，substring始终会将较小的参数作为开始位置。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> str = <span class="string">"Wearefamily"</span>; <span class="comment">//length为11</span></div><div class="line"><span class="built_in">console</span>.log(str.slice(<span class="number">-3</span>,<span class="number">-8</span>));<span class="comment">//相当于str.slice(8,3)//返回空字符串</span></div><div class="line"><span class="built_in">console</span>.log(str.slice(<span class="number">-5</span>,<span class="number">-1</span>));<span class="comment">//相当于str.slice(6,10)//返回ami</span></div><div class="line"><span class="built_in">console</span>.log(str.substring(<span class="number">3</span>,<span class="number">-4</span>));<span class="comment">//相当于str.substring(3,0)//返回Wea</span></div><div class="line"><span class="built_in">console</span>.log(str.substring(<span class="number">-3</span>,<span class="number">-5</span>));<span class="comment">//相当于str.substring(0,0)//返回空字符串</span></div><div class="line"><span class="built_in">console</span>.log(str.substr(<span class="number">-4</span>,<span class="number">3</span>));<span class="comment">//相当于substr(7,3)//返回mil</span></div><div class="line"><span class="built_in">console</span>.log(str.substr(<span class="number">-4</span>,<span class="number">-3</span>));<span class="comment">//相当于substr(7,0)//返回空字符串</span></div></pre></td></tr></table></figure>
<h1 id="replace"><a href="#replace" class="headerlink" title="replace()"></a>replace()</h1><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">str.replace(regexp|substr, newSubStr|<span class="function"><span class="keyword">function</span>)</span></div></pre></td></tr></table></figure>
<p>替换 str 的子字符串</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> str = <span class="string">"do you love me"</span>;</div><div class="line">str.replace(<span class="string">'love'</span>,<span class="string">'hate'</span>); <span class="comment">// "do you hate me"</span></div></pre></td></tr></table></figure>
<h1 id="search"><a href="#search" class="headerlink" title="search()"></a>search()</h1><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">str.search(regexp)</div></pre></td></tr></table></figure>
<p>查找 str 与一个正则表达式是否匹配。如果匹配成功，则返回正则表达式在字符串中首次匹配项的索引；否则，返回 -1。如果参数传入的是一个非正则表达式对象，则会使用 new RegExp(obj) 隐式地将其转换为正则表达式对象</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> str = <span class="string">'I love JavaScript!'</span>;</div><div class="line">str.search(<span class="regexp">/java/</span>); <span class="comment">// -1</span></div><div class="line">str.search(<span class="regexp">/Java/</span>); <span class="comment">// 7</span></div><div class="line">str.search(<span class="regexp">/java/i</span>); <span class="comment">// 7</span></div><div class="line">str.search(<span class="string">'Java'</span>); <span class="comment">// 7</span></div></pre></td></tr></table></figure>
<h1 id="match"><a href="#match" class="headerlink" title="match()"></a>match()</h1><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">str.match(regexp)</div></pre></td></tr></table></figure>
<p>返回一个包含匹配结果的数组，如果没有匹配项，则返回 null。如果参数传入的是一个非正则表达式对象，则会使用 new RegExp(obj) 隐式地将其转换为正则表达式对象</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> str = <span class="string">'Javascript java'</span>;</div><div class="line">str.match(<span class="regexp">/Java/</span>); <span class="comment">// ["Java"]</span></div><div class="line"><span class="comment">//全局匹配+忽略大小写</span></div><div class="line">str.match(<span class="regexp">/Java/gi</span>); <span class="comment">// ["java", "Java"]</span></div><div class="line">str.match(<span class="regexp">/ab/g</span>); <span class="comment">// null</span></div></pre></td></tr></table></figure>
<h1 id="spilt"><a href="#spilt" class="headerlink" title="spilt()"></a>spilt()</h1><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">str.split([separator][, limit])</div></pre></td></tr></table></figure>
<p>返回一个数组，分隔符 separator 可以是一个字符串或正则表达式</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> str = <span class="string">"Hello?World!"</span>;</div><div class="line"><span class="comment">//第一个参数：指定分隔的地方</span></div><div class="line">str.split(); <span class="comment">// ["Hello?World!"]</span></div><div class="line">str.split(<span class="string">''</span>); <span class="comment">// ["H", "e", "l", "l", "o", "?", "W", "o", "r", "l", "d", "!"]</span></div><div class="line">str.split(<span class="string">'?'</span>); <span class="comment">// ["Hello", "World!"]</span></div><div class="line"><span class="comment">//第二个参数：返回指定数组的长度</span></div><div class="line">str.split(<span class="string">''</span>,<span class="number">5</span>); <span class="comment">// ["H", "e", "l", "l", "o"]</span></div></pre></td></tr></table></figure>
<h1 id="trim"><a href="#trim" class="headerlink" title="trim()"></a>trim()</h1><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">str.trim()</div></pre></td></tr></table></figure>
<p>去除 str 开头和结尾处的空白字符，返回 str 的一个副本，不影响字符串本身的值</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> str = <span class="string">' abc '</span>;</div><div class="line">str.trim(); <span class="comment">// 'abc'</span></div><div class="line"><span class="built_in">console</span>.log(str); <span class="comment">// ' abc '</span></div></pre></td></tr></table></figure>
<h1 id="toLowerCase"><a href="#toLowerCase" class="headerlink" title="toLowerCase()"></a>toLowerCase()</h1><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">str.toLowerCase()</div></pre></td></tr></table></figure>
<p>将 str 转换为小写，并返回 str 的一个副本，不影响字符串本身的值</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> str = <span class="string">'JavaScript'</span>;</div><div class="line">str.toLowerCase(); <span class="comment">// 'javascript'</span></div><div class="line"><span class="built_in">console</span>.log(str); <span class="comment">// 'JavaScript'</span></div></pre></td></tr></table></figure>
<h1 id="toUpperCase"><a href="#toUpperCase" class="headerlink" title="toUpperCase()"></a>toUpperCase()</h1><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">str.toUpperCase()</div></pre></td></tr></table></figure>
<p>将 str 转换为大写，并返回 str 的一个副本，不影响字符串本身的值</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> str = <span class="string">'JavaScript'</span>;</div><div class="line">str.toUpperCase(); <span class="comment">// 'JAVASCRIPT'</span></div><div class="line"><span class="built_in">console</span>.log(str); <span class="comment">// 'JavaScript'</span></div></pre></td></tr></table></figure>
<h1 id="concact"><a href="#concact" class="headerlink" title="concact()"></a>concact()</h1><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> a = <span class="string">"my "</span>;</div><div class="line"><span class="built_in">console</span>.log(a.concat(<span class="string">"future"</span>));<span class="comment">//my future</span></div><div class="line"><span class="built_in">console</span>.log(a.concat(<span class="string">"future "</span>, <span class="string">"will"</span>));<span class="comment">//my future will</span></div></pre></td></tr></table></figure>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/javascript/" rel="tag"># javascript</a>
          
            <a href="/tags/string/" rel="tag"># string</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2016/12/03/README/" rel="next" title="如何搭建个人博客">
                <i class="fa fa-chevron-left"></i> 如何搭建个人博客
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2016/12/22/JS数组常用方法/" rel="prev" title="JS中数组的常用方法">
                JS中数组的常用方法 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="http://dynamic-image.yesky.com/300x-/uploadImages/upload/20140912/upload/201409/wylnwjynkxijpg.jpg"
               alt="shine-wl" />
          <p class="site-author-name" itemprop="name">shine-wl</p>
           
              <p class="site-description motion-element" itemprop="description"></p>
          
        </div>
        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
            
              <a href="/archives/">
            
                <span class="site-state-item-count">21</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          

          
            
            
            <div class="site-state-item site-state-tags">
              
                <span class="site-state-item-count">15</span>
                <span class="site-state-item-name">标签</span>
              
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#chatAt"><span class="nav-number">1.</span> <span class="nav-text">chatAt()</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#charCodeAt"><span class="nav-number">2.</span> <span class="nav-text">charCodeAt()</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#fromCharCode"><span class="nav-number">3.</span> <span class="nav-text">fromCharCode( )</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#indexOf"><span class="nav-number">4.</span> <span class="nav-text">indexOf()</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#lastIndexOf"><span class="nav-number">5.</span> <span class="nav-text">lastIndexOf()</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#substring"><span class="nav-number">6.</span> <span class="nav-text">substring()</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#slice"><span class="nav-number">7.</span> <span class="nav-text">slice()</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#substr"><span class="nav-number">8.</span> <span class="nav-text">substr()</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#substring-slice-substr-三者的区别"><span class="nav-number">9.</span> <span class="nav-text">substring() slice() substr()三者的区别</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#replace"><span class="nav-number">10.</span> <span class="nav-text">replace()</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#search"><span class="nav-number">11.</span> <span class="nav-text">search()</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#match"><span class="nav-number">12.</span> <span class="nav-text">match()</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#spilt"><span class="nav-number">13.</span> <span class="nav-text">spilt()</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#trim"><span class="nav-number">14.</span> <span class="nav-text">trim()</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#toLowerCase"><span class="nav-number">15.</span> <span class="nav-text">toLowerCase()</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#toUpperCase"><span class="nav-number">16.</span> <span class="nav-text">toUpperCase()</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#concact"><span class="nav-number">17.</span> <span class="nav-text">concact()</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">shine-wl</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动</div>

  <span class="post-meta-divider">|</span>

  <div class="theme-info">主题 &mdash; <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.2</div>


        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.2"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.2"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.2"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.2"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.2"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.2"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.2"></script>



  


  




	





  





  








  





  

  

  

  

  

  

</body>
</html>
